/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.util;

import java.io.IOException;
import java.util.Vector;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

/**
 * <p>
 * Description: 
 * </p>
 * 
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-7-10 下午12:43:55          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class TrapGeneratorUtil {

    private Snmp snmp = null;

    private Address targetAddress = null;

    public void initComm() throws IOException {

        // 设置管理进程的IP和端口   
        targetAddress = GenericAddress.parse("udp:localhost/162");
        TransportMapping transport = new DefaultUdpTransportMapping();
        snmp = new Snmp(transport);
        transport.listen();
    }

    public void sendPDU() throws IOException {

        // 设置 target   
        CommunityTarget target = new CommunityTarget();
        target.setAddress(targetAddress);

        // 通信不成功时的重试次数   
        target.setRetries(2);
        // 超时时间   
        target.setTimeout(1500);
        // snmp版本   
        target.setVersion(SnmpConstants.version2c);
        target.setCommunity(new OctetString("BSM_EXT_EVENT"));

        // 创建 PDU   
        PDU pdu = new PDU();
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.1"), new OctetString("SnmpTrap")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.2"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.21"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.22"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.23"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.24"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.25"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.26"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.27"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.28"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.29"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.211"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.222"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.223"), new OctetString("JavaEE")));
        pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.234"), new OctetString("JavaEE")));
        pdu.setType(PDU.TRAP);

        // 向Agent发送PDU，并接收Response   
        ResponseEvent respEvnt = snmp.send(pdu, target);
        // 解析Response   
        if (respEvnt != null && respEvnt.getResponse() != null) {
            Vector<VariableBinding> recVBs = respEvnt.getResponse().getVariableBindings();
            for (int i = 0; i < recVBs.size(); i++) {
                VariableBinding recVB = recVBs.elementAt(i);
                System.out.println(recVB.getOid() + " : " + recVB.getVariable());
            }
        }
    }

    public static void main(String[] args) {
        try {
            TrapGeneratorUtil util = new TrapGeneratorUtil();
            util.initComm();
            util.sendPDU();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
